package com.future.lock.home.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.future.lock.R;
import com.future.lock.home.activity.EditPeopleNickActivity;
import com.future.lock.home.entity.bean.LockNotes;

import java.sql.BatchUpdateException;
import java.util.List;

/**
 * Created by jniu on 2017/11/25.
 */

public class LockNotesAdapter extends BaseSectionQuickAdapter<LockNotes,BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param layoutResId      The layout resource id of each item.
     * @param sectionHeadResId The section head layout id for each item
     * @param data             A new list is created out of this one to avoid mutable list
     */
    public LockNotesAdapter(int layoutResId, int sectionHeadResId, List<LockNotes> data) {
        super(layoutResId, sectionHeadResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, LockNotes item) {
        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append(item.t.user);
//        builder.setSpan(new ForegroundColorSpan(Color.BLUE),0, builder.length() , SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
        builder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                int position = (int) widget.getTag();
                Intent intent = new Intent(mContext, EditPeopleNickActivity.class);
                intent.putExtra("un_user_id",getData().get(position).t.unlock_user_id);
                intent.putExtra("remark",getData().get(position).t.user);
                mContext.startActivity(intent);
            }
        }, 0, item.t.user.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        builder.append(item.t.content);
        ((TextView) helper.getView(R.id.tv_content)).setMovementMethod(LinkMovementMethod.getInstance());
        ((TextView) helper.getView(R.id.tv_content)).setTag(helper.getAdapterPosition());
        helper.setText(R.id.tv_time,item.t.time)
                .setText(R.id.tv_content,builder)
                .setImageResource(R.id.item_iv,item.t.isError?R.mipmap.ic_lock_notes_error:R.mipmap.ic_lock_notes_ok);
        helper.setVisible(R.id.top_line,true).setVisible(R.id.bottom_line,true);
        if (getData().get(helper.getAdapterPosition()-1).isHeader) {
            helper.setVisible(R.id.top_line,false);
        }
        if (helper.getAdapterPosition() == getData().size()-1){
            helper.setVisible(R.id.bottom_line,false);
        }else{
            if (getData().get(helper.getAdapterPosition()+1).isHeader) {
                helper.setVisible(R.id.bottom_line,false);
            }
        }
    }

    @Override
    protected void convertHead(BaseViewHolder helper, LockNotes item) {
        helper.setText(R.id.tv_head,item.header);
    }
}
